home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / src / stencil.c < prev    next >
C/C++ Source or Header  |  1999-02-04  |  23KB  |  1,007 lines

  1. /* $Id: stencil.c,v 3.2 1998/03/27 04:17:31 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.0
  6.  * Copyright (C) 1995-1998  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: stencil.c,v $
  26.  * Revision 3.2  1998/03/27 04:17:31  brianp
  27.  * fixed G++ warnings
  28.  *
  29.  * Revision 3.1  1998/02/08 20:17:42  brianp
  30.  * removed unneeded headers
  31.  *
  32.  * Revision 3.0  1998/01/31 21:03:42  brianp
  33.  * initial rev
  34.  *
  35.  */
  36.  
  37.  
  38. #ifdef PC_HEADER
  39. #include "all.h"
  40. #else
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include "context.h"
  44. #include "macros.h"
  45. #include "pb.h"
  46. #include "stencil.h"
  47. #include "types.h"
  48. #endif
  49.  
  50.  
  51. /*
  52.  * Return the address of a stencil buffer value given the window coords:
  53.  */
  54. #define STENCIL_ADDRESS(X,Y)  (ctx->Buffer->Stencil + ctx->Buffer->Width * (Y) + (X))
  55.  
  56.  
  57. void gl_ClearStencil( GLcontext *ctx, GLint s )
  58. {
  59.    if (INSIDE_BEGIN_END(ctx)) {
  60.       gl_error( ctx, GL_INVALID_OPERATION, "glClearStencil" );
  61.       return;
  62.    }
  63.    ctx->Stencil.Clear = (GLstencil) s;
  64. }
  65.  
  66.  
  67.  
  68. void gl_StencilFunc( GLcontext *ctx, GLenum func, GLint ref, GLuint mask )
  69. {
  70.    GLint maxref;
  71.  
  72.    if (INSIDE_BEGIN_END(ctx)) {
  73.       gl_error( ctx, GL_INVALID_OPERATION, "glStencilFunc" );
  74.       return;
  75.    }
  76.  
  77.    switch (func) {
  78.       case GL_NEVER:
  79.       case GL_LESS:
  80.       case GL_LEQUAL:
  81.       case GL_GREATER:
  82.       case GL_GEQUAL:
  83.       case GL_EQUAL:
  84.       case GL_NOTEQUAL:
  85.       case GL_ALWAYS:
  86.          ctx->Stencil.Function = func;
  87.          break;
  88.       default:
  89.          gl_error( ctx, GL_INVALID_ENUM, "glStencilFunc" );
  90.          return;
  91.    }
  92.  
  93.    maxref = (1 << STENCIL_BITS) - 1;
  94.    ctx->Stencil.Ref = CLAMP( ref, 0, maxref );
  95.    ctx->Stencil.ValueMask = mask;
  96. }
  97.  
  98.  
  99.  
  100. void gl_StencilMask( GLcontext *ctx, GLuint mask )
  101. {
  102.    if (INSIDE_BEGIN_END(ctx)) {
  103.       gl_error( ctx, GL_INVALID_OPERATION, "glStencilMask" );
  104.       return;
  105.    }
  106.    ctx->Stencil.WriteMask = (GLstencil) mask;
  107. }
  108.  
  109.  
  110.  
  111. void gl_StencilOp( GLcontext *ctx, GLenum fail, GLenum zfail, GLenum zpass )
  112. {
  113.    if (INSIDE_BEGIN_END(ctx)) {
  114.       gl_error( ctx, GL_INVALID_OPERATION, "glStencilOp" );
  115.       return;
  116.    }
  117.    switch (fail) {
  118.       case GL_KEEP:
  119.       case GL_ZERO:
  120.       case GL_REPLACE:
  121.       case GL_INCR:
  122.       case GL_DECR:
  123.       case GL_INVERT:
  124.          ctx->Stencil.FailFunc = fail;
  125.          break;
  126.       default:
  127.          gl_error( ctx, GL_INVALID_ENUM, "glStencilOp" );
  128.          return;
  129.    }
  130.    switch (zfail) {
  131.       case GL_KEEP:
  132.       case GL_ZERO:
  133.       case GL_REPLACE:
  134.       case GL_INCR:
  135.       case GL_DECR:
  136.       case GL_INVERT:
  137.          ctx->Stencil.ZFailFunc = zfail;
  138.          break;
  139.       default:
  140.          gl_error( ctx, GL_INVALID_ENUM, "glStencilOp" );
  141.          return;
  142.    }
  143.    switch (zpass) {
  144.       case GL_KEEP:
  145.       case GL_ZERO:
  146.       case GL_REPLACE:
  147.       case GL_INCR:
  148.       case GL_DECR:
  149.       case GL_INVERT:
  150.          ctx->Stencil.ZPassFunc = zpass;
  151.          break;
  152.       default:
  153.          gl_error( ctx, GL_INVALID_ENUM, "glStencilOp" );
  154.          return;
  155.    }
  156. }
  157.  
  158.  
  159.  
  160. /* Stencil Logic:
  161.  
  162. IF stencil test fails THEN
  163.    Don't write the pixel (RGBA,Z)
  164.    Execute FailOp
  165. ELSE
  166.    Write the pixel
  167. ENDIF
  168.  
  169. Perform Depth Test
  170.  
  171. IF depth test passes OR no depth buffer THEN
  172.    Execute ZPass
  173.    Write the pixel
  174. ELSE
  175.    Execute ZFail
  176. ENDIF
  177.  
  178. */
  179.  
  180.  
  181.  
  182.  
  183. /*
  184.  * Apply the given stencil operator for each pixel in the span whose
  185.  * mask flag is set.
  186.  * Input:  n - number of pixels in the span
  187.  *         x, y - location of leftmost pixel in the span
  188.  *         oper - the stencil buffer operator
  189.  *         mask - array [n] of flag:  1=apply operator, 0=don't apply operator
  190.  */
  191. static void apply_stencil_op_to_span( GLcontext *ctx,
  192.                                       GLuint n, GLint x, GLint y,
  193.                       GLenum oper, GLubyte mask[] )
  194. {
  195.    GLuint i;
  196.    GLstencil s, ref;
  197.    GLstencil wrtmask, invmask;
  198.    GLstencil *stencil;
  199.  
  200.    wrtmask = ctx->Stencil.WriteMask;
  201.    invmask = ~ctx->Stencil.WriteMask;
  202.    ref = ctx->Stencil.Ref;
  203.    stencil = STENCIL_ADDRESS( x, y );
  204.  
  205.    switch (oper) {
  206.       case GL_KEEP:
  207.          /* do nothing */
  208.          break;
  209.       case GL_ZERO:
  210.      if (invmask==0) {
  211.         for (i=0;i<n;i++) {
  212.            if (mask[i]) {
  213.           stencil[i] = 0;
  214.            }
  215.         }
  216.      }
  217.      else {
  218.         for (i=0;i<n;i++) {
  219.            if (mask[i]) {
  220.           stencil[i] = stencil[i] & invmask;
  221.            }
  222.         }
  223.      }
  224.      break;
  225.       case GL_REPLACE:
  226.      if (invmask==0) {
  227.         for (i=0;i<n;i++) {
  228.            if (mask[i]) {
  229.                   stencil[i] = ref;
  230.            }
  231.         }
  232.      }
  233.      else {
  234.         for (i=0;i<n;i++) {
  235.            if (mask[i]) {
  236.           s = stencil[i];
  237.           stencil[i] = (invmask & s ) | (wrtmask & ref);
  238.            }
  239.         }
  240.      }
  241.      break;
  242.       case GL_INCR:
  243.      if (invmask==0) {
  244.         for (i=0;i<n;i++) {
  245.            if (mask[i]) {
  246.           s = stencil[i];
  247.           if (s<0xff) {
  248.              stencil[i] = s+1;
  249.           }
  250.            }
  251.         }
  252.      }
  253.      else {
  254.         for (i=0;i<n;i++) {
  255.            if (mask[i]) {
  256.           /* VERIFY logic of adding 1 to a write-masked value */
  257.           s = stencil[i];
  258.           if (s<0xff) {
  259.              stencil[i] = (invmask & s) | (wrtmask & (s+1));
  260.           }
  261.            }
  262.         }
  263.      }
  264.      break;
  265.       case GL_DECR:
  266.      if (invmask==0) {
  267.         for (i=0;i<n;i++) {
  268.            if (mask[i]) {
  269.           s = stencil[i];
  270.           if (s>0) {
  271.              stencil[i] = s-1;
  272.           }
  273.            }
  274.         }
  275.      }
  276.      else {
  277.         for (i=0;i<n;i++) {
  278.            if (mask[i]) {
  279.           /* VERIFY logic of subtracting 1 to a write-masked value */
  280.           s = stencil[i];
  281.           if (s>0) {
  282.              stencil[i] = (invmask & s) | (wrtmask & (s-1));
  283.           }
  284.            }
  285.         }
  286.      }
  287.      break;
  288.       case GL_INVERT:
  289.      if (invmask==0) {
  290.         for (i=0;i<n;i++) {
  291.            if (mask[i]) {
  292.           s = stencil[i];
  293.           stencil[i] = ~s;
  294.            }
  295.         }
  296.      }
  297.      else {
  298.         for (i=0;i<n;i++) {
  299.            if (mask[i]) {
  300.           s = stencil[i];
  301.           stencil[i] = (invmask & s) | (wrtmask & ~s);
  302.            }
  303.         }
  304.      }
  305.      break;
  306.       default:
  307.          gl_problem(ctx, "Bad stencilop in apply_stencil_op_to_span");
  308.    }
  309. }
  310.  
  311.  
  312.  
  313.  
  314. /*
  315.  * Apply stencil test to a span of pixels before depth buffering.
  316.  * Input:  n - number of pixels in the span
  317.  *         x, y - coordinate of left-most pixel in the span
  318.  *         mask - array [n] of flag:  0=skip the pixel, 1=stencil the pixel
  319.  * Output:  mask - pixels which fail the stencil test will have their
  320.  *                 mask flag set to 0.
  321.  * Return:  0 = all pixels failed, 1 = zero or more pixels passed.
  322.  */
  323. GLint gl_stencil_span( GLcontext *ctx,
  324.                        GLuint n, GLint x, GLint y, GLubyte mask[] )
  325. {
  326.    GLubyte fail[MAX_WIDTH];
  327.    GLint allfail = 0;
  328.    GLuint i;
  329.    GLstencil r, s;
  330.    GLstencil *stencil;
  331.  
  332.    stencil = STENCIL_ADDRESS( x, y );
  333.  
  334.    /*
  335.     * Perform stencil test.  The results of this operation are stored
  336.     * in the fail[] array:
  337.     *   IF fail[i] is non-zero THEN
  338.     *       the stencil fail operator is to be applied
  339.     *   ELSE
  340.     *       the stencil fail operator is not to be applied
  341.     *   ENDIF
  342.     */
  343.    switch (ctx->Stencil.Function) {
  344.       case GL_NEVER:
  345.          /* always fail */
  346.          for (i=0;i<n;i++) {
  347.         if (mask[i]) {
  348.            mask[i] = 0;
  349.            fail[i] = 1;
  350.         }
  351.         else {
  352.            fail[i] = 0;
  353.         }
  354.      }
  355.      allfail = 1;
  356.      break;
  357.       case GL_LESS:
  358.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  359.      for (i=0;i<n;i++) {
  360.         if (mask[i]) {
  361.            s = stencil[i] & ctx->Stencil.ValueMask;
  362.            if (r < s) {
  363.           /* passed */
  364.           fail[i] = 0;
  365.            }
  366.            else {
  367.           fail[i] = 1;
  368.           mask[i] = 0;
  369.            }
  370.         }
  371.         else {
  372.            fail[i] = 0;
  373.         }
  374.      }
  375.      break;
  376.       case GL_LEQUAL:
  377.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  378.      for (i=0;i<n;i++) {
  379.         if (mask[i]) {
  380.            s = stencil[i] & ctx->Stencil.ValueMask;
  381.            if (r <= s) {
  382.           /* pass */
  383.           fail[i] = 0;
  384.            }
  385.            else {
  386.           fail[i] = 1;
  387.           mask[i] = 0;
  388.            }
  389.         }
  390.         else {
  391.            fail[i] = 0;
  392.         }
  393.      }
  394.      break;
  395.       case GL_GREATER:
  396.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  397.      for (i=0;i<n;i++) {
  398.         if (mask[i]) {
  399.            s = stencil[i] & ctx->Stencil.ValueMask;
  400.            if (r > s) {
  401.           /* passed */
  402.           fail[i] = 0;
  403.            }
  404.            else {
  405.           fail[i] = 1;
  406.           mask[i] = 0;
  407.            }
  408.         }
  409.         else {
  410.            fail[i] = 0;
  411.         }
  412.      }
  413.      break;
  414.       case GL_GEQUAL:
  415.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  416.      for (i=0;i<n;i++) {
  417.         if (mask[i]) {
  418.            s = stencil[i] & ctx->Stencil.ValueMask;
  419.            if (r >= s) {
  420.           /* passed */
  421.           fail[i] = 0;
  422.            }
  423.            else {
  424.           fail[i] = 1;
  425.           mask[i] = 0;
  426.            }
  427.         }
  428.         else {
  429.            fail[i] = 0;
  430.         }
  431.      }
  432.      break;
  433.       case GL_EQUAL:
  434.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  435.      for (i=0;i<n;i++) {
  436.         if (mask[i]) {
  437.            s = stencil[i] & ctx->Stencil.ValueMask;
  438.            if (r == s) {
  439.           /* passed */
  440.           fail[i] = 0;
  441.            }
  442.            else {
  443.           fail[i] = 1;
  444.           mask[i] = 0;
  445.            }
  446.         }
  447.         else {
  448.            fail[i] = 0;
  449.         }
  450.      }
  451.      break;
  452.       case GL_NOTEQUAL:
  453.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  454.      for (i=0;i<n;i++) {
  455.         if (mask[i]) {
  456.            s = stencil[i] & ctx->Stencil.ValueMask;
  457.            if (r != s) {
  458.           /* passed */
  459.           fail[i] = 0;
  460.            }
  461.            else {
  462.           fail[i] = 1;
  463.           mask[i] = 0;
  464.            }
  465.         }
  466.         else {
  467.            fail[i] = 0;
  468.         }
  469.      }
  470.      break;
  471.       case GL_ALWAYS:
  472.      /* always pass */
  473.      for (i=0;i<n;i++) {
  474.         fail[i] = 0;
  475.      }
  476.      break;
  477.       default:
  478.          gl_problem(ctx, "Bad stencil func in gl_stencil_span");
  479.          return 0;
  480.    }
  481.  
  482.    apply_stencil_op_to_span( ctx, n, x, y, ctx->Stencil.FailFunc, fail );
  483.  
  484.    return (allfail) ? 0 : 1;
  485. }
  486.  
  487.  
  488.  
  489.  
  490. /*
  491.  * Apply the combination depth-buffer/stencil operator to a span of pixels.
  492.  * Input:  n - number of pixels in the span
  493.  *         x, y - location of leftmost pixel in span
  494.  *         z - array [n] of z values
  495.  * Input:  mask - array [n] of flags  (1=test this pixel, 0=skip the pixel)
  496.  * Output:  mask - array [n] of flags (1=depth test passed, 0=failed) 
  497.  */
  498. void gl_depth_stencil_span( GLcontext *ctx,
  499.                             GLuint n, GLint x, GLint y, const GLdepth z[],
  500.                 GLubyte mask[] )
  501. {
  502.    if (ctx->Depth.Test==GL_FALSE) {
  503.       /*
  504.        * No depth buffer, just apply zpass stencil function to active pixels.
  505.        */
  506.       apply_stencil_op_to_span( ctx, n, x, y, ctx->Stencil.ZPassFunc, mask );
  507.    }
  508.    else {
  509.       /*
  510.        * Perform depth buffering, then apply zpass or zfail stencil function.
  511.        */
  512.       GLubyte passmask[MAX_WIDTH], failmask[MAX_WIDTH], oldmask[MAX_WIDTH];
  513.       GLuint i;
  514.  
  515.       /* init pass and fail masks to zero, copy mask[] to oldmask[] */
  516.       for (i=0;i<n;i++) {
  517.      passmask[i] = failmask[i] = 0;
  518.          oldmask[i] = mask[i];
  519.       }
  520.  
  521.       /* apply the depth test */
  522.       if (ctx->Driver.DepthTestSpan)
  523.          (*ctx->Driver.DepthTestSpan)( ctx, n, x, y, z, mask );
  524.  
  525.       /* set the stencil pass/fail flags according to result of depth test */
  526.       for (i=0;i<n;i++) {
  527.          if (oldmask[i]) {
  528.             if (mask[i]) {
  529.                passmask[i] = 1;
  530.             }
  531.             else {
  532.                failmask[i] = 1;
  533.             }
  534.          }
  535.       }
  536.  
  537.       /* apply the pass and fail operations */
  538.       apply_stencil_op_to_span( ctx, n, x, y, ctx->Stencil.ZFailFunc, failmask );
  539.       apply_stencil_op_to_span( ctx, n, x, y, ctx->Stencil.ZPassFunc, passmask );
  540.    }
  541. }
  542.  
  543.  
  544.  
  545.  
  546. /*
  547.  * Apply the given stencil operator for each pixel in the array whose
  548.  * mask flag is set.
  549.  * Input:  n - number of pixels in the span
  550.  *         x, y - array of [n] pixels
  551.  *         operator - the stencil buffer operator
  552.  *         mask - array [n] of flag:  1=apply operator, 0=don't apply operator
  553.  */
  554. static void apply_stencil_op_to_pixels( GLcontext *ctx,
  555.                                         GLuint n, const GLint x[],
  556.                         const GLint y[],
  557.                         GLenum oper, GLubyte mask[] )
  558. {
  559.    GLuint i;
  560.    GLstencil ref;
  561.    GLstencil wrtmask, invmask;
  562.  
  563.    wrtmask = ctx->Stencil.WriteMask;
  564.    invmask = ~ctx->Stencil.WriteMask;
  565.  
  566.    ref = ctx->Stencil.Ref;
  567.  
  568.    switch (oper) {
  569.       case GL_KEEP:
  570.          /* do nothing */
  571.          break;
  572.       case GL_ZERO:
  573.      if (invmask==0) {
  574.         for (i=0;i<n;i++) {
  575.            if (mask[i]) {
  576.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  577.                   *sptr = 0;
  578.            }
  579.         }
  580.      }
  581.      else {
  582.         for (i=0;i<n;i++) {
  583.            if (mask[i]) {
  584.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  585.           *sptr = invmask & *sptr;
  586.            }
  587.         }
  588.      }
  589.      break;
  590.       case GL_REPLACE:
  591.      if (invmask==0) {
  592.         for (i=0;i<n;i++) {
  593.            if (mask[i]) {
  594.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  595.                   *sptr = ref;
  596.            }
  597.         }
  598.      }
  599.      else {
  600.         for (i=0;i<n;i++) {
  601.            if (mask[i]) {
  602.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  603.           *sptr = (invmask & *sptr ) | (wrtmask & ref);
  604.            }
  605.         }
  606.      }
  607.      break;
  608.       case GL_INCR:
  609.      if (invmask==0) {
  610.         for (i=0;i<n;i++) {
  611.            if (mask[i]) {
  612.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  613.           if (*sptr < 0xff) {
  614.              *sptr = *sptr + 1;
  615.           }
  616.            }
  617.         }
  618.      }
  619.      else {
  620.         for (i=0;i<n;i++) {
  621.            if (mask[i]) {
  622.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  623.           if (*sptr<0xff) {
  624.              *sptr = (invmask & *sptr) | (wrtmask & (*sptr+1));
  625.           }
  626.            }
  627.         }
  628.      }
  629.      break;
  630.       case GL_DECR:
  631.      if (invmask==0) {
  632.         for (i=0;i<n;i++) {
  633.            if (mask[i]) {
  634.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  635.           if (*sptr>0) {
  636.              *sptr = *sptr - 1;
  637.           }
  638.            }
  639.         }
  640.      }
  641.      else {
  642.         for (i=0;i<n;i++) {
  643.            if (mask[i]) {
  644.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  645.           if (*sptr>0) {
  646.              *sptr = (invmask & *sptr) | (wrtmask & (*sptr-1));
  647.           }
  648.            }
  649.         }
  650.      }
  651.      break;
  652.       case GL_INVERT:
  653.      if (invmask==0) {
  654.         for (i=0;i<n;i++) {
  655.            if (mask[i]) {
  656.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  657.                   *sptr = ~*sptr;
  658.            }
  659.         }
  660.      }
  661.      else {
  662.         for (i=0;i<n;i++) {
  663.            if (mask[i]) {
  664.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  665.                   *sptr = (invmask & *sptr) | (wrtmask & ~*sptr);
  666.            }
  667.         }
  668.      }
  669.      break;
  670.       default:
  671.          gl_problem(ctx, "Bad stencilop in apply_stencil_op_to_pixels");
  672.    }
  673. }
  674.  
  675.  
  676.  
  677. /*
  678.  * Apply stencil test to an array of pixels before depth buffering.
  679.  * Input:  n - number of pixels in the span
  680.  *         x, y - array of [n] pixels to stencil
  681.  *         mask - array [n] of flag:  0=skip the pixel, 1=stencil the pixel
  682.  * Output:  mask - pixels which fail the stencil test will have their
  683.  *                 mask flag set to 0.
  684.  * Return:  0 = all pixels failed, 1 = zero or more pixels passed.
  685.  */
  686. GLint gl_stencil_pixels( GLcontext *ctx,
  687.                          GLuint n, const GLint x[], const GLint y[],
  688.              GLubyte mask[] )
  689. {
  690.    GLubyte fail[PB_SIZE];
  691.    GLstencil r, s;
  692.    GLuint i;
  693.    GLint allfail = 0;
  694.  
  695.    /*
  696.     * Perform stencil test.  The results of this operation are stored
  697.     * in the fail[] array:
  698.     *   IF fail[i] is non-zero THEN
  699.     *       the stencil fail operator is to be applied
  700.     *   ELSE
  701.     *       the stencil fail operator is not to be applied
  702.     *   ENDIF
  703.     */
  704.  
  705.    switch (ctx->Stencil.Function) {
  706.       case GL_NEVER:
  707.          /* always fail */
  708.          for (i=0;i<n;i++) {
  709.         if (mask[i]) {
  710.            mask[i] = 0;
  711.            fail[i] = 1;
  712.         }
  713.         else {
  714.            fail[i] = 0;
  715.         }
  716.      }
  717.      allfail = 1;
  718.      break;
  719.       case GL_LESS:
  720.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  721.      for (i=0;i<n;i++) {
  722.         if (mask[i]) {
  723.                GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
  724.            s = *sptr & ctx->Stencil.ValueMask;
  725.            if (r < s) {
  726.           /* passed */
  727.           fail[i] = 0;
  728.            }
  729.            else {
  730.           fail[i] = 1;
  731.           mask[i] = 0;
  732.            }
  733.         }
  734.         else {
  735.            fail[i] = 0;
  736.         }
  737.      }
  738.      break;
  739.       case GL_LEQUAL:
  740.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  741.      for (i=0;i<n;i++) {
  742.         if (mask[i]) {
  743.                GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
  744.            s = *sptr & ctx->Stencil.ValueMask;
  745.            if (r <= s) {
  746.           /* pass */
  747.           fail[i] = 0;
  748.            }
  749.            else {
  750.           fail[i] = 1;
  751.           mask[i] = 0;
  752.            }
  753.         }
  754.         else {
  755.            fail[i] = 0;
  756.         }
  757.      }
  758.      break;
  759.       case GL_GREATER:
  760.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  761.      for (i=0;i<n;i++) {
  762.         if (mask[i]) {
  763.                GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
  764.            s = *sptr & ctx->Stencil.ValueMask;
  765.            if (r > s) {
  766.           /* passed */
  767.           fail[i] = 0;
  768.            }
  769.            else {
  770.           fail[i] = 1;
  771.           mask[i] = 0;
  772.            }
  773.         }
  774.         else {
  775.            fail[i] = 0;
  776.         }
  777.      }
  778.      break;
  779.       case GL_GEQUAL:
  780.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  781.      for (i=0;i<n;i++) {
  782.         if (mask[i]) {
  783.                GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
  784.            s = *sptr & ctx->Stencil.ValueMask;
  785.            if (r >= s) {
  786.           /* passed */
  787.           fail[i] = 0;
  788.            }
  789.            else {
  790.           fail[i] = 1;
  791.           mask[i] = 0;
  792.            }
  793.         }
  794.         else {
  795.            fail[i] = 0;
  796.         }
  797.      }
  798.      break;
  799.       case GL_EQUAL:
  800.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  801.      for (i=0;i<n;i++) {
  802.         if (mask[i]) {
  803.                GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
  804.            s = *sptr & ctx->Stencil.ValueMask;
  805.            if (r == s) {
  806.           /* passed */
  807.           fail[i] = 0;
  808.            }
  809.            else {
  810.           fail[i] = 1;
  811.           mask[i] = 0;
  812.            }
  813.         }
  814.         else {
  815.            fail[i] = 0;
  816.         }
  817.      }
  818.      break;
  819.       case GL_NOTEQUAL:
  820.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  821.      for (i=0;i<n;i++) {
  822.         if (mask[i]) {
  823.                GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
  824.            s = *sptr & ctx->Stencil.ValueMask;
  825.            if (r != s) {
  826.           /* passed */
  827.           fail[i] = 0;
  828.            }
  829.            else {
  830.           fail[i] = 1;
  831.           mask[i] = 0;
  832.            }
  833.         }
  834.         else {
  835.            fail[i] = 0;
  836.         }
  837.      }
  838.      break;
  839.       case GL_ALWAYS:
  840.      /* always pass */
  841.      for (i=0;i<n;i++) {
  842.         fail[i] = 0;
  843.      }
  844.      break;
  845.       default:
  846.          gl_problem(ctx, "Bad stencil func in gl_stencil_pixels");
  847.          return 0;
  848.    }
  849.  
  850.    apply_stencil_op_to_pixels( ctx, n, x, y, ctx->Stencil.FailFunc, fail );
  851.  
  852.    return (allfail) ? 0 : 1;
  853. }
  854.  
  855.  
  856.  
  857.  
  858. /*
  859.  * Apply the combination depth-buffer/stencil operator to a span of pixels.
  860.  * Input:  n - number of pixels in the span
  861.  *         x, y - array of [n] pixels to stencil
  862.  *         z - array [n] of z values
  863.  * Input:  mask - array [n] of flags  (1=test this pixel, 0=skip the pixel)
  864.  * Output:  mask - array [n] of flags (1=depth test passed, 0=failed) 
  865.  */
  866. void gl_depth_stencil_pixels( GLcontext *ctx,
  867.                               GLuint n, const GLint x[], const GLint y[],
  868.                   const GLdepth z[], GLubyte mask[] )
  869. {
  870.    if (ctx->Depth.Test==GL_FALSE) {
  871.       /*
  872.        * No depth buffer, just apply zpass stencil function to active pixels.
  873.        */
  874.       apply_stencil_op_to_pixels( ctx, n, x, y, ctx->Stencil.ZPassFunc, mask );
  875.    }
  876.    else {
  877.       /*
  878.        * Perform depth buffering, then apply zpass or zfail stencil function.
  879.        */
  880.       GLubyte passmask[PB_SIZE], failmask[PB_SIZE], oldmask[PB_SIZE];
  881.       GLuint i;
  882.  
  883.       /* init pass and fail masks to zero */
  884.       for (i=0;i<n;i++) {
  885.      passmask[i] = failmask[i] = 0;
  886.          oldmask[i] = mask[i];
  887.       }
  888.  
  889.       /* apply the depth test */
  890.       if (ctx->Driver.DepthTestPixels)
  891.          (*ctx->Driver.DepthTestPixels)( ctx, n, x, y, z, mask );
  892.  
  893.       /* set the stencil pass/fail flags according to result of depth test */
  894.       for (i=0;i<n;i++) {
  895.          if (oldmask[i]) {
  896.             if (mask[i]) {
  897.                passmask[i] = 1;
  898.             }
  899.             else {
  900.                failmask[i] = 1;
  901.             }
  902.          }
  903.       }
  904.  
  905.       /* apply the pass and fail operations */
  906.       apply_stencil_op_to_pixels( ctx, n, x, y,
  907.                                   ctx->Stencil.ZFailFunc, failmask );
  908.       apply_stencil_op_to_pixels( ctx, n, x, y,
  909.                                   ctx->Stencil.ZPassFunc, passmask );
  910.    }
  911.  
  912. }
  913.  
  914.  
  915.  
  916. /*
  917.  * Return a span of stencil values from the stencil buffer.
  918.  * Input:  n - how many pixels
  919.  *         x,y - location of first pixel
  920.  * Output:  stencil - the array of stencil values
  921.  */
  922. void gl_read_stencil_span( GLcontext *ctx,
  923.                            GLuint n, GLint x, GLint y, GLubyte stencil[] )
  924. {
  925.    GLstencil *s;
  926.  
  927.    if (ctx->Buffer->Stencil) {
  928.       s = STENCIL_ADDRESS( x, y );
  929.       MEMCPY( stencil, s, n * sizeof(GLubyte) );
  930.    }
  931. }
  932.  
  933.  
  934.  
  935. /*
  936.  * Write a span of stencil values to the stencil buffer.
  937.  * Input:  n - how many pixels
  938.  *         x,y - location of first pixel
  939.  *         stencil - the array of stencil values
  940.  */
  941. void gl_write_stencil_span( GLcontext *ctx,
  942.                             GLuint n, GLint x, GLint y,
  943.                 const GLubyte stencil[] )
  944. {
  945.    GLstencil *s;
  946.  
  947.    if (ctx->Buffer->Stencil) {
  948.       s = STENCIL_ADDRESS( x, y );
  949.       MEMCPY( s, stencil, n * sizeof(GLubyte) );
  950.    }
  951. }
  952.  
  953.  
  954.  
  955. /*
  956.  * Allocate a new stencil buffer.  If there's an old one it will be
  957.  * deallocated first.  The new stencil buffer will be uninitialized.
  958.  */
  959. void gl_alloc_stencil_buffer( GLcontext *ctx )
  960. {
  961.    GLuint buffersize = ctx->Buffer->Width * ctx->Buffer->Height;
  962.  
  963.    /* deallocate current stencil buffer if present */
  964.    if (ctx->Buffer->Stencil) {
  965.       free(ctx->Buffer->Stencil);
  966.       ctx->Buffer->Stencil = NULL;
  967.    }
  968.  
  969.    /* allocate new stencil buffer */
  970.    ctx->Buffer->Stencil = (GLstencil *) malloc(buffersize * sizeof(GLstencil));
  971.    if (!ctx->Buffer->Stencil) {
  972.       /* out of memory */
  973.       ctx->Stencil.Enabled = GL_FALSE;
  974.       gl_error( ctx, GL_OUT_OF_MEMORY, "gl_alloc_stencil_buffer" );
  975.    }
  976. }
  977.  
  978.  
  979.  
  980.  
  981. /*
  982.  * Clear the stencil buffer.  If the stencil buffer doesn't exist yet we'll
  983.  * allocate it now.
  984.  */
  985. void gl_clear_stencil_buffer( GLcontext *ctx )
  986. {
  987.    if (ctx->Visual->StencilBits==0 || !ctx->Buffer->Stencil) {
  988.       /* no stencil buffer */
  989.       return;
  990.    }
  991.  
  992.    if (ctx->Scissor.Enabled) {
  993.       /* clear scissor region only */
  994.       GLint y;
  995.       GLint width = ctx->Buffer->Xmax - ctx->Buffer->Xmin + 1;
  996.       for (y=ctx->Buffer->Ymin; y<=ctx->Buffer->Ymax; y++) {
  997.          GLstencil *ptr = STENCIL_ADDRESS( ctx->Buffer->Xmin, y );
  998.          MEMSET( ptr, ctx->Stencil.Clear, width * sizeof(GLstencil) );
  999.       }
  1000.    }
  1001.    else {
  1002.       /* clear whole stencil buffer */
  1003.       MEMSET( ctx->Buffer->Stencil, ctx->Stencil.Clear,
  1004.               ctx->Buffer->Width * ctx->Buffer->Height * sizeof(GLstencil) );
  1005.    }
  1006. }
  1007.